<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Export\ExchangeExport;
use App\Http\Controllers\Admin\Business\Repository\Combo;
use App\Models\ComboExchange;

use App\Repositories\ExchangeRepository;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;

class ComboExchangeController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('套餐兑换码');
            $content->description('套餐兑换码');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(ComboExchange::class, function (Grid $grid) {
            $grid->model()->orderBy('id','desc');
            $grid->id('id')->sortable();
            $grid->combo_id('套餐名称')->display(function($value){
                return Combo::find($value)->title ?? '无标题';
            });
            $grid->code('兑换码');
            $grid->created_at('生成时间');
            $grid->column('status','是否使用')->display(function($value){
                return $value == 0 ? '未使用' : '已使用';
            });
            $grid->exporter(new ExchangeExport());
            $grid->disableActions();
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });
            $grid->filter(function($filter){
                $filter->disableIdFilter();
                $filter->equal('combo_id','套餐名称')->select(Combo::getSelectOptions());
                $filter->equal('status','状态')->select([0=>'未使用',1=>'已使用']);
                $filter->between('created_at', '生成时间')->datetime();


            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(ComboExchange::class, function (Form $form) {

            $form->select('combo_id','套餐名称')->options(Combo::getSelectOptions());
            $form->number('num','生成数量');
            $form->saving(function($form){
                //仓库
               $exchange= new ExchangeRepository();
               $exchange->createdExchange($form->combo_id,$form->num);
                $success = new MessageBag([
                    'title'   => '生成兑换码成功',
                    'message' => 'success',
                ]);
                return redirect()->to('/administrator/exchange')->with(compact('success'));
            });


        });
    }
}
